package com.sgb.alimaplibrary.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.sgb.alimaplibrary.R;
import com.swgk.core.util.MToast;

import java.io.File;

public class NavUtil {

    //调起导航的uri
    private static final String BAIDU_MAP_NAVI_URI = "baidumap://map/navi?query=";
    private static final String GAODE_MAP_NAVI_URI = "androidamap://navi?sourceApplication=";
    //map app包名
    private static final String BAIDU_MAP_APP = "com.baidu.BaiduMap";
    private static final String GAODE_MAP_APP = "com.autonavi.minimap";

    private static final String QQ_MAP_URL = "http://apis.map.qq.com/uri/v1/routeplan?type=drive&";
    //高德web服务的临时key(用于地理编码)
    private static final String SELF_AMAP_KEY = "your key";
    private static final String GEOCODE_HTTP_URL = "http://restapi.amap.com/v3/geocode/geo?";
    public static final String TENXUN = "ten_xun";
    public static final String GAODE = "gao_de";
    public static final String BAIDU = "bai_du";

    /**
     * by moos on 2017/09/18
     * func:调起高德导航
     *
     * @param lat   纬度
     * @param lon   经度
     * @param address
     *
     * dev 是否偏移(0:lat 和 lon 是已经加密后的,不需要国测加密; 1:需要国测加密)
     * style 导航方式(0 速度快; 1 费用少; 2 路程短; 3 不走高速；4 躲避拥堵；5 不走高速且避免收费；6 不走高速且躲避拥堵；7 躲避收费和拥堵；8 不走高速躲避收费和拥堵)
     */
    public static void goNaviByGaoDeMap(Activity activity, String lat, String lon, String address) {
        String url = "androidamap://route?sourceApplication=" + R.string.app_name + "&sname=我的位置&dlat=" + lat + "&dlon=" + lon + "&dname=" + address + "&dev=0&m=0&t=0";
        Intent intent = new Intent("android.intent.action.VIEW",
                android.net.Uri.parse(url));
        activity.startActivity(intent);
    }

    /**
     * by moos on 2017/09/18
     * func:调起百度导航
     */
    public static void goNaviByBaiDuMap(Activity activity, String address) {
        Intent intent = new Intent();
        intent.setData(Uri.parse(BAIDU_MAP_NAVI_URI + address));
        activity.startActivity(intent);
    }

    /**
     * by moos on 2021/01/18
     * func:通过网页形式调起腾讯地图
     *
     *  startPoint
     *  endPoint
     *  policy     规划策略(0:快捷,1:无高速,2:距离短)
     *  appName    当前应用的名称
     */
    public static void goNaviByTencentMap(Activity activity,String startPoint, String endPoint) {
        Uri uri = Uri.parse(QQ_MAP_URL + "from="+startPoint + "&to=" + endPoint + "&policy="+ 0 + "&referer="+ R.string.app_name );
        Intent intent = new Intent(Intent.ACTION_VIEW,uri);
        activity.startActivity(intent);
    }

    /**
     * by moos on 2017/09/18
     * func:接受导航请求,并发起导航
     */
    public static void navigateForDestination(Activity activity, String Key, String endPoint, String lat, String lon,String startPotion) {
        if (GAODE.equals(Key)) {
            if (isApplicationInstall(GAODE_MAP_APP))
                //安装了高德map
                goNaviByGaoDeMap(activity, lat, lon, endPoint);
            else
                showInstallAppTip(activity);
        } else if (BAIDU.equals(Key)) {
            //安装了百度map
            if (isApplicationInstall(BAIDU_MAP_APP))
                goNaviByBaiDuMap(activity, endPoint);
            else
                showInstallAppTip(activity);
        } else if (TENXUN.equals(Key)) {
            //使用腾讯网页地图(可选)
            goNaviByTencentMap(activity,startPotion, endPoint);
        }
    }


    /**
     * by moos on 2017/09/18
     * func:判断手机是否安装了该应用
     *
     * @param packageName
     * @return
     */
    private static boolean isApplicationInstall(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }

    /**
     * by moos on 2017/09/18
     * func:安装地图app的提示
     */
    private static void showInstallAppTip(Activity activity) {

        LayoutInflater inflater = LayoutInflater.from(activity);
        View layout = inflater.inflate(R.layout.tip_dialog, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setView(layout);
        builder.setCancelable(true);
        AlertDialog dialog = builder.show();
        TextView tvCancel = layout.findViewById(R.id.tv_cancel);
        TextView tvConfirm = layout.findViewById(R.id.tv_confirm);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Intent intent;
                Uri uri = Uri.parse("market://details?id=com.autonavi.minimap");
                intent = new Intent(Intent.ACTION_VIEW, uri);
                activity.startActivity(intent);
            }
        });
        dialog.show();
    }
}
